package com.sky.zzh.analyse;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.sky.zzh.R;
import com.sky.zzh.util.CommonUtil;

/**
 * 今日数据
 * 
 * @author steveyan
 * 
 */
@SuppressLint("ValidFragment")
public class AnaChartFragment extends Fragment {

	protected String[] mMonths = new String[] { "", "", "", "", "", "", "" };

	// 常量
	private static final String ARG_PARAM1 = "param1";
	private static final String ARG_PARAM2 = "param2";
	private static final String ARG_PARAM3 = "param3";
	private JSONArray mJsonArray;

	// 接收的参数
	private int type;
	private String date;
	private String longest;

	// fragment和activity的通信接口
	// private OnFragmentInteractionListener mListener = null;
	private BarChart barChart;

	// X轴显示的内容
	private ArrayList<String> xVals;

	public AnaChartFragment() {
	}
	
	public AnaChartFragment(JSONArray jsonArray) {
		this.mJsonArray = jsonArray;
	}

	//
	public static AnaChartFragment newInstance(String date,JSONArray jsonArray,String longest) {
		AnaChartFragment fragment = new AnaChartFragment(jsonArray);
		System.out.println("aa:"+jsonArray);
		Bundle args = new Bundle();
		// args.putInt(ARG_PARAM1, type);
		args.putString(ARG_PARAM2, date);
		args.putString(ARG_PARAM3, longest);
//		args.putString("jsonArray", jsonArray.toString());
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			type = getArguments().getInt(ARG_PARAM1);
			date = getArguments().getString(ARG_PARAM2);
			longest = getArguments().getString(ARG_PARAM3);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragmen_getup, container, false);
		barChart = (BarChart) view.findViewById(R.id.chartGetUp);
		barChart.setDrawBarShadow(false);
		barChart.setDrawValueAboveBar(true);
		barChart.setDescription("");
		barChart.setPinchZoom(false);
		barChart.setDrawGridBackground(false);
		barChart.setTouchEnabled(false);
		barChart.animateY(2000);
//		barChart.setMaxVisibleValueCount(4);
//		barChart

		// 设置周边的指示
		XAxis xAxis = barChart.getXAxis();
		xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
		xAxis.setDrawGridLines(false);
		xAxis.setSpaceBetweenLabels(2);
		xAxis.setAxisLineColor(getActivity().getResources().getColor(R.color.color_blue_an));
		xAxis.setTextColor(getActivity().getResources().getColor(R.color.color_charttextcolor));

		YAxis leftAxis = barChart.getAxisLeft();
		leftAxis.setLabelCount(4, true);
		leftAxis.setAxisLineColor(getActivity().getResources().getColor(R.color.color_blue_an));
//		leftAxis.setYOffset(2.0f);
//		leftAxis.setAxisMaxValue(10.0f); // 设置Y轴最大值
//		leftAxis.setAxisMinValue(4.0f);// 设置Y轴最小值。
		// leftAxis.setStartAtZero(false);//柱状图的起画点
		// leftAxis.set
		// leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
//		 leftAxis.setSpaceTop(30f);
		if(CommonUtil.isEmpty(longest)){
			leftAxis.setAxisMaxValue(10);
		}else{
			leftAxis.setAxisMaxValue((int)Math.ceil(Double.valueOf(longest)));
		}
		 leftAxis.setAxisMinValue(4);
		 leftAxis.setStartAtZero(false);
		leftAxis.setDrawGridLines(false);
		leftAxis.setTextColor(getActivity().getResources().getColor(R.color.color_blue_an));
//		leftAxis.set
		// leftAxis.setAxisMinValue(4);
		// leftAxis.setAxisMaxValue(10);
		ValueFormatter custom = new MyValueFormatter();
		leftAxis.setValueFormatter(custom);
		
		// 关掉左边的
		leftAxis.setEnabled(true);
		YAxis rightAxis = barChart.getAxisRight();
		// rightAxis.setDrawGridLines(false);
		// rightAxis.setLabelCount(8, false);
		// rightAxis.setSpaceTop(15f);

		// 关闭右边的指示
		rightAxis.setEnabled(false);

		// 设置示例图标
		// Legend l = barChart.getLegend();
		// l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
		// l.setForm(Legend.LegendForm.SQUARE);
		// l.setFormSize(9f);
		// l.setTextSize(11f);
		// l.setXEntrySpace(4f);
		//

		// 关闭示例图标
		barChart.getLegend().setEnabled(false);

		setData(7, 10);
		return view;
	}

	private void setData(int count, float range) {
		xVals = new ArrayList<String>();
		for (int i = 0; i < count; i++) {
			xVals.add(mMonths[i % 12]);
		}

		ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

		for (int i = 0; i < mJsonArray.length(); i++) {
//			float mult = (range + 1);
//			float val = (float) (Math.random() * mult);
//			if(i==0){
//				System.out.println("b:"+mJsonArray.optJSONObject(i).optString("SleepDuration"));
//			}
//			System.out.println("i:"+i);
			yVals1.add(new BarEntry(Float.valueOf(mJsonArray.optJSONObject(i).optString("SleepDuration")), i));
		}

		BarDataSet set1 = new BarDataSet(yVals1, "DataSet");
		set1.setBarSpacePercent(80f);
		set1.setColor(getActivity().getResources().getColor(R.color.color_green));
		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(set1);

		BarData data = new BarData(xVals, dataSets);
		data.setValueTextSize(0f);
		barChart.clear();
		barChart.setData(data);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	// @Override
	// public void onAttach(Activity activity) {
	// super.onAttach(activity);
	// try {
	// mListener = (OnFragmentInteractionListener) activity;
	// } catch (ClassCastException e) {
	// throw new ClassCastException(activity.toString()
	// + " must implement OnFragmentInteractionListener");
	// }
	// }
	//
	// @Override
	// public void onDetach() {
	// super.onDetach();
	// mListener = null;
	// }

}
